package com.sdms.core.rbac;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Role
{
	public static final Role CUSTOMER = new Role( Right.R_ADD_ITEM, Right.R_VIEW_ITEM );
	public static final Role DELIVERY_AGENT = new Role( Right.R_VIEW_ITEM, Right.R_VIEW_LIST, Right.R_UPDATE_ITEM );
	public static final Role ADMINISTRATOR = new Role( Right.R_VIEW_ITEM, Right.R_CREATE_USER, Right.R_REMOVE_USER, Right.R_CHANGE_PREVILAGE );
	public static final Role MANAGER = new Role( Right.R_VIEW_ITEM, Right.R_VIEW_LIST, Right.R_CANCEL_DELIVERY, Right.R_UPDATE_ITEM, Right.R_CHANGE_PREVILAGE );

	private List<Right> assignedRights;
	private List<Right> upgradeRights;
	private boolean isUpgraded;

	private Role( Right... assignedRights )
	{
		List<Right> rights = Arrays.asList( assignedRights );
		this.assignedRights = new ArrayList<Right>();
		this.upgradeRights = new ArrayList<Right>();
		for ( Right right : rights )
		{
			if ( right.isUpgrade() )
			{
				this.upgradeRights.add( right );
			}
			else
			{
				this.assignedRights.add( right );
			}
		}
	}

	private Role( Role r )
	{
		this.assignedRights = r.getAssignedRights();
		this.upgradeRights = r.getUpgradeRights();
	}

	public List<Right> getAssignedRights()
	{
		return assignedRights;
	}

	public List<Right> getUpgradeRights()
	{
		return upgradeRights;
	}

	public boolean isUpgraded()
	{
		return isUpgraded;
	}

	public void setUpgraded( boolean isUpgraded )
	{
		this.isUpgraded = isUpgraded;
	}

	public Role clone()
	{
		return new Role( this );
	}
}
